$(function(){

    function init(){
        $("#list-table .sub-menu-block:even").addClass("even");//所有奇数行添加类even
        //激活拖拽排序
        $("#list-table .sub-menu-table").sortable({
            containerSelector: 'table',
            itemPath: '> tbody',
            itemSelector: 'tr',
            placeholder: '<tr class="placeholder"/>'
        });
        bindEvent();
    }

    function bindEvent(){
        //复选框click事件
        $("#list-table .is_use").click(function(){
            if($(this).get(0).checked==true){
                //启用菜单
                $(this).val("1");
            }else{
                //停用菜单
                $(this).val("0");
            }
        });

        //保存
        $("#list-table .save").click(function(){
			var ids = [];
            var names = [];
            var unique_names = [];
            var icons = [];
			var is_uses = [];
            var teams = [];//菜单分组
            var sorts = [];//菜单排序系数
            //顶级菜单组
            $("#list-table table.sub-menu-table").each(function(i){//顶级菜单排序索引
                var topMenu = $(this);
                //顶级菜单块
                $(".sub-menu-block",topMenu).each(function(j){//二级菜单块分组排序索引，从0开始
                    //二级菜单块
                    var block = $(this);
                    //菜单分组块里有记录
                    $(".menu",block).each(function(k){//菜单排序索引，从0开始
                        var menu = $(this);
                        //将数字0、1、2、3、4映射成a、b、c、d...
                        //0的ASCII码为48
                        var code = 48 + j + 49;//获取映射后的字母字符ASCII码
                        var team = String.fromCharCode(code);//转换成字母字符
                        var name = $.trim(menu.find(".menu-name").val());//菜单名称
                        var unique_name = $.trim(menu.find(".menu-unique-name").val());//菜单别名
                        var icon = $.trim(menu.find(".menu-icon").val());
                        var sort = k + 1;
                        
                        ids.push(menu.attr("data-id"));
                        is_uses.push(menu.find(".is_use").val());
                        names.push(name);
                        unique_names.push(unique_name);
                        teams.push(team);
                        sorts.push(sort);
                        icons.push(icon);

                    });
                });


            });

            $.ajax(
                {
                    type:"post",
                    url:"saveMenu",
                    data:{"ids":ids.join(","),"names":names.join(","),"unique_names":unique_names.join(","),"icons":icons.join(","),"is_uses":is_uses.join(","),"teams":teams.join(","),"sorts":sorts.join(",")},
                    dataType:"json",
                    success:function(json,jsonText){
                        if(json.errorCode == 0){
                            $("#myModal .modal-body").html("<p class='text-success'><b>恭喜您，操作成功！</b></p>");
                            $("#myModal").modal('show');
                            //定时器，1.5秒后模态框自动关闭
                            setTimeout(function(){
                                $("#myModal").modal('hide');
                            },1000);
                        }else{
                            //alert("添加失败，请稍后再试！");
                            $("#myModal .modal-body").html("<p class='text-danger'>很抱歉，操作失败，发生异常！</p>");
                            $("#myModal").modal('show');
                            //定时器，1.5秒后模态框自动关闭
                            setTimeout(function(){
                                $("#myModal").modal('hide');
                            },1000);
                        }
                    },
                    error:function(){
                        $("#myModal .modal-body").html("<p class='text-danger'>很抱歉，操作失败，请求出错！</p>");
                        $("#myModal").modal('show');
                        //定时器，1.5秒后模态框自动关闭
                        setTimeout(function(){
                            $("#myModal").modal('hide');
                        },1000);
                    }
                }
            );

        });

    }
    init();
});